/*
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2022-03-02 13:18:13
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-03-03 16:23:57
 */

/**
 * time：时间
 * num：循环次数
 * type：循环类型  1：天，2:周，3:月，4:年
 * frequency：循环类型的时间段，如每一天，每两周，每三月
*/
import {
    formatDate
} from '@/utils/index'
function getMouthArr(time, frequency) {
    let getTime = ''
    let mouth = new Date(time).getMonth() + 1
    let year = new Date(time).getFullYear()
    let day = new Date(time).getDate()

    let mouth1 = mouth + parseInt(frequency)
    console.log(mouth1);
    if (mouth1 <= 12) {
        mouth = mouth1
        mouth = mouth < 10 ? '0' + mouth : mouth
        day = day < 10 ? '0' + day : day
        getTime = year + '-' + mouth + '-' + day
    } else if (mouth1 > 12) {
        let remainder = mouth1 % 12
        let multiple = (mouth1 - remainder) / 12
        year = year + multiple
        mouth = remainder
        mouth = mouth < 10 ? '0' + mouth : mouth
        day = day < 10 ? '0' + day : day
        getTime = year + '-' + mouth + '-' + day
    }
    // console.log(time, frequency);
    return getTime
}
export function timeToggle(time, num, type, frequency) {
    let timeArr = []
    let timeDate = new Date(time).valueOf()  //传入时间戳
    let dayStamp = 60 * 60 * 24 * 1000  //一天的时间戳
    let weekStamp = dayStamp * 7 //一个星期的时间戳

    // 获取当前年份
    let year = new Date(time).getFullYear()
    let yearStamp = ''
    if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0) {
        yearStamp = dayStamp * 366
    } else {
        yearStamp = dayStamp * 365
    }
    // 获取循环所有时间数组
    if (type == 1) {
        for (var i = 0; i < num; i++) {
            let time = formatDate(timeDate + (dayStamp * i * frequency)).split(' ')[0]
            timeArr.push(time)
        }
    } else if (type == 2) {
        for (var i = 0; i < num; i++) {
            let time = formatDate(timeDate + (weekStamp * i * frequency)).split(' ')[0]
            timeArr.push(time)
        }
    } else if (type == 3) {
        for (var i = 0; i < num; i++) {
            if (timeArr.length == 0) {
                timeArr.push(time)
            } else {
                let timeValue = getMouthArr(timeArr[i - 1], frequency)
                timeArr.push(timeValue)
            }
        }
    } else if (type == 4) {
        for (var i = 0; i < num; i++) {
            let time = formatDate(timeDate + (yearStamp * i * frequency)).split(' ')[0]
            timeArr.push(time)
        }
    }
    console.log(timeArr);
    return timeArr
}